package com.onlyxiahui.extend.spring.webmvc.dispatcher.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

/**
 * <br>
 * Date 2019-12-03 17:32:35<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */
@Configuration
@Import({ OnlyActionDispatcherPortal.class })
public class OnlyDispatcherStartConfig {

	private OnlyPathMatchConfigurer pathMatchConfigurer;

	/**
	 * Override to plug a sub-class of {@link RequestMappingHandlerMapping}.
	 */
	protected OnlyActionMappingHandlerMapping createActionMappingHandlerMapping() {
		return new OnlyActionMappingHandlerMapping();
	}

	/**
	 * Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
	 * requests to annotated controllers.
	 */
	@Bean
	public OnlyActionMappingHandlerMapping actionMappingHandlerMapping(
			@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
			@Qualifier("mvcConversionService") FormattingConversionService conversionService,
			@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {

		OnlyActionMappingHandlerMapping mapping = createActionMappingHandlerMapping();
		mapping.setOrder(0);
		// mapping.setInterceptors(getInterceptors(conversionService,
		// resourceUrlProvider));
		mapping.setContentNegotiationManager(contentNegotiationManager);
		// mapping.setCorsConfigurations(getCorsConfigurations());

//		PathMatchConfigurer configurer = getPathMatchConfigurer();
//
//		Boolean useSuffixPatternMatch = configurer.isUseSuffixPatternMatch();
//		if (useSuffixPatternMatch != null) {
//			mapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
//		}
//		Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch();
//		if (useRegisteredSuffixPatternMatch != null) {
//			mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch);
//		}
//		Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
//		if (useTrailingSlashMatch != null) {
//			mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
//		}
//
//		UrlPathHelper pathHelper = configurer.getUrlPathHelper();
//		if (pathHelper != null) {
//			mapping.setUrlPathHelper(pathHelper);
//		}
//		PathMatcher pathMatcher = configurer.getPathMatcher();
//		if (pathMatcher != null) {
//			mapping.setPathMatcher(pathMatcher);
//		}
//		Map<String, Predicate<Class<?>>> pathPrefixes = configurer.getPathPrefixes();
//		if (pathPrefixes != null) {
//			mapping.setPathPrefixes(pathPrefixes);
//		}

		return mapping;
	}

	/**
	 * Callback for building the {@link PathMatchConfigurer}. This method is final,
	 * use {@link #configurePathMatching} to customize path matching.
	 */
	protected final OnlyPathMatchConfigurer getPathMatchConfigurer() {
		if (this.pathMatchConfigurer == null) {
			this.pathMatchConfigurer = new OnlyPathMatchConfigurer();
			configurePathMatching(this.pathMatchConfigurer);
		}
		return this.pathMatchConfigurer;
	}

	/**
	 * Override to configure path matching options.
	 */
	public void configurePathMatching(OnlyPathMatchConfigurer configurer) {
	}
}
